﻿using System.Reflection;
using Caliburn.Micro;
using Caliburn.Micro.Recipes.Filters.Framework;
using SS.WPF.Framework.Display.Dialog;
using SS.WPF.Framework.ExceptionHandling;
using SS.WPF.Framework.Selection;
using IEventAggregator = Microsoft.Practices.Composite.Events.IEventAggregator;
using EventAggregator = Microsoft.Practices.Composite.Events.EventAggregator;

namespace TestFramework
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition.Primitives;
    using System.Linq;

    public class AppBootstrapper : Bootstrapper<IShell>
    {
        private CompositionContainer _container;

        protected override void Configure()
        {
            // Discovery ViewModels in Main project 
            var catalog = new AggregateCatalog(
                  this.SelectAssemblies().Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());

            // Discovery Modules in a Directory 
            var directoryCatalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory, "SS.WPF.Framework.*");
            catalog.Catalogs.Add(directoryCatalog);

            // Refer to the MEF container
            this._container = new CompositionContainer(catalog);
            var batch = new CompositionBatch();

            ServiceRegistrar.Register(this._container);

            batch.AddExportedValue<IWindowManager>(new WindowManager());
            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
            batch.AddExportedValue<ISelectionService>(new SelectionService());
            batch.AddExportedValue<IExceptionProcessor>(new ExceptionProcessor());
            batch.AddExportedValue(catalog);

            this._container.Compose(batch);

            // Hook the filter framework
            ActionMessage.EnforceGuardsDuringInvocation = true;
            FilterFrameworkCoreCustomization.Hook();
        }

        protected override object GetInstance(Type serviceType, string key)
        {
            var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
            var exports = _container.GetExportedValues<object>(contract);

            if (exports.Any())
            {
                return exports.First();
            }

            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
        }

        //protected override void BuildUp(object instance)
        //{
        //    _container.SatisfyImportsOnce(instance);
        //}

        protected override IEnumerable<Assembly> SelectAssemblies()
        {
            var assembly = new List<Assembly>(base.SelectAssemblies())
                {
                    typeof(DialogManager).Assembly
                };
            return assembly;
        }
    }
}